<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return['diy']=$this->plugin_setting('diy');
$basic=$this->plugin_setting('basic');
$return['basic']=$basic;
$return['banner']=$basic['hdps'];
$this->session('type',$_RQ['type']);
if($_RQ['type']){
	$this->session('type',$_RQ['type']);
}
if($_RQ['op']=='main'){
	$this->model('model')->checklogin();
	if($_RQ['type']=='work'){
		$this->model('model')->checkvip();
	}
	//公告列表
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$notice_list = pdo_getslice('jy_weishop_notice',array('uniacid'=>$_SESSION['uniacid'],'status'=>1),array($pindex,20),$total,array('id','title','createtime'),'','id desc');
	if($notice_list){
		foreach($notice_list as &$notice){
			if($_SESSION['os']=='windows'){
				$notice['createtime']=date('m-d',$notice['createtime']);
			}else{
				$notice['createtime']=date('Y-m-d h:i',$notice['createtime']);
			}
		}
	}
	$return['notices']=$notice_list;
	set_html('title',$basic['title']?$basic['title']:'报价平台');
	$return['sharedata']=array(
		'title'=>$basic['share_title'],
		'desc'=>$basic['share_desc'],
		'link'=>custom_url('jy_shopbaojia/index//fromuser='.$_SESSION['uid'],true),
		'imgUrl'=>ATTACHMENT_URL.$basic['share_image'],
	);
	$return['params']=array('groupid'=>0,'keyword'=>'','ishot'=>1,'isall'=>1);
	if($_RQ['keyword']){
		$return['params']['keyword']=$_RQ['keyword'];
	}
	$return['groups']=pdo_getall('jy_shopbaojia_group',array('uniacid'=>$_SESSION['uniacid'],'status'=>'1'),'','','paixu desc');
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$params=array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'noshow'=>0);	
	$fields=array('id','title','desc','image','group_id','ishot','price','readnum','createtime');
	foreach($return['groups'] as &$item){
		$params['group_id']=$item['id'];
		$list=pdo_getslice('jy_shopbaojia_list',$params,array($pindex,$psize),$total,$fields,'','updatetime desc');
		foreach($list as &$lv){
			$lv['image']=tomedia($lv['image']);
		}
		$item['list']=$list;
	}
	//是否关注
	$return['isfollow']=pdo_getcolumn('core_fans',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'follow');
	$return['placement_id']=$basic['ad_id1'];
	exi($return);
}
if($_RQ['op']=='getlist'){
	$return['params']=array();
	$params=array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'noshow'=>0);	
	if($_RQ['mid']){
		$params['mid']=intval($_RQ['mid']);	
	}
	if($_RQ['ishot']){
		$params['ishot']=intval($_RQ['ishot']);	
	}
	if($_RQ['groupid']){
		$params['group_id']=intval($_RQ['groupid']);	
	}
	if($_RQ['pcate']){
		$params['group_id']=intval($_RQ['pcate']);	
	}
	if($_RQ['ccate']){
		$params['cid']=intval($_RQ['ccate']);	
	}
	if($_RQ['keyword']){
		$isnum=pdo_count('jy_shopbaojia_list',array('title like'=>'%'.$_RQ['keyword'].'%','uniacid'=>$_SESSION['uniacid'],'status'=>1,'noshow'=>0),'id');
		if($isnum<=0){
			$params['productsn like']='%'.$_RQ['keyword'].'%';
		}else{
			$params['title like']='%'.$_RQ['keyword'].'%';
		}
		$return['params']['keyword']=$_RQ['keyword'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 30;
	$list=pdo_getslice('jy_shopbaojia_list',$params,array($pindex,$psize),$total,$fields,'','productsn desc,updatetime desc');
	foreach($list as &$kv){
		$kv['thumb']=tomedia($kv['thumb']);
	}
	$newlist=array();
	if($list){
		$sum=count($list);
		$lat_lng['lng']=$_RQ['lng'];
		$lat_lng['lat']=$_RQ['lat'];
		foreach($list as $item){
			$item['updatetime']=$item['updatetime']?:$item['createtime'];
			$item['createtime']=date('Y-m-d H:i:s',$item['createtime']);
			$member=pdo_get('core_members',array('id'=>$item['mid'],'uniacid'=>$_SESSION['uniacid']),array('nickname','avatar'));
			$item['avatar']=$item['avatar']?$item['avatar']:$member['avatar'];
			$item['thumb']=tomedia($item['thumb']);
			$lasttime=TIMESTAMP-$item['updatetime'];
			if($lasttime>24*3600){
				$item['updatetimetitle']=intval($lasttime/(24*3600)).'天前';
			}elseif($lasttime>=3600){
				$item['updatetimetitle']=intval($lasttime/3600).'小时前';
			}else{
				$item['updatetimetitle']=intval($lasttime/60).'分钟前';
			}
			$shoucang=pdo_get('jy_shopbaojia_shoucang',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'listid'=>$item['id']));
			$item['islike']=$shoucang['isshoucang']?1:0;
			$item['likes']=pdo_count('jy_shopbaojia_shoucang',array('listid'=>$item['id'],'uniacid'=>$_SESSION['uniacid'],'isshoucang'=>1),'id');
			$item['merchname']=pdo_getcolumn('jy_merch_user',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['merchid']),'merchname');
			$item['typetitle']=$this->content_types[$item['isnews']];
			if($this->setting['kan_type']){
				$adlog=pdo_get('jy_shopbaojia_adlog',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'mid'=>$_SESSION['uid'],'listid'=>$item['id']),'','createtime desc');
				if($adlog['createtime'] && $adlog['createtime']>TIMESTAMP-6*3600){
					$item['canlook']=1;
				}
			}else{
				$item['canlook']=1;
			}
			$merch=pdo_get('jy_merch_user',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['merchid']),array('id','lat_lng','pcd','address'));
			$merch['lat_lng']=json_decode($merch['lat_lng'],true);
			$merch['pcd']=json_decode($merch['pcd'],true);
			$item['merch']=$merch;
			if($lat_lng['lng'] && $lat_lng['lat'] && $merch['lat_lng'] && $merch['lat_lng']['lng'] && $merch['lat_lng']['lat']){
				$item['juli']=cfc('tools')->getdistance($lat_lng['lng'],$lat_lng['lat'],$merch['lat_lng']['lng'],$merch['lat_lng']['lat']);
				$item['juli']=intval($item['juli']/1000);
			}
			if($item['productsn']){
				$newlist[$item['productsn']]['title']=$item['title'];
				$newlist[$item['productsn']]['productsn']=$item['productsn'];
				$newlist[$item['productsn']]['list'][]=$item;
			}else{
				$newlist[$item['id']]['title']=$item['title'];
				$newlist[$item['id']]['productsn']='';
				$newlist[$item['id']]['list'][]=$item;
			}
		}
		$list=array();
		foreach($newlist as &$nv){
			$list[]=$nv;
		}
	}
	$return['list']=$list;
	$return['params']['page'] =$pindex;
	exi($return);
}
if($_RQ['op']=='gethot'){
	$return['params']=array();
	$params=array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'noshow'=>0);	
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$list=pdo_getslice('jy_shopbaojia_list',$params,array($pindex,$psize),$total,$fields,'','updatetime desc');
	foreach($list as &$kv){
			$kv['thumb']=tomedia($kv['thumb']);
	}
	//按点赞数排序作品
	if($list){
		$sum=count($list);
		foreach($list as &$item){
			$item['updatetime']=$item['updatetime']?:$item['createtime'];
			$item['createtime']=date('Y-m-d H:i:s',$item['createtime']);
			$member=pdo_get('core_members',array('id'=>$item['mid'],'uniacid'=>$_SESSION['uniacid']),array('nickname','avatar'));
			$item['avatar']=$item['avatar']?$item['avatar']:$member['avatar'];
			$item['thumb']=tomedia($item['thumb']);
			$lasttime=TIMESTAMP-$item['updatetime'];
			if($lasttime>24*3600){
				$item['updatetimetitle']=intval($lasttime/24*3600).'天前';
			}elseif($lasttime>=3600){
				$item['updatetimetitle']=intval($lasttime/3600).'小时前';
			}else{
				$item['updatetimetitle']=intval($lasttime/60).'分钟前';
			}
			$item['likes']=pdo_count('jy_shopbaojia_shoucang',array('listid'=>$item['id'],'uniacid'=>$_SESSION['uniacid'],'isshoucang'=>1),'id');
			$item['typetitle']=$this->content_types[$item['isnews']];
		}
	}
	$return['list']=$list;
	exi($return);
}
if($_RQ['op']=='setlike'){
	$listid=intval($_RQ['id']);
	$shoucang=pdo_get('jy_shopbaojia_shoucang',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'listid'=>$listid));
	if(!$shoucang['id']){
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'listid'=>$listid,
			'isshoucang'=>1,
			'createtime'=>TIMESTAMP
		);
		pdo_insert('jy_shopbaojia_shoucang',$insert);
		exi('收藏成功！');
	}else{
		pdo_update('jy_shopbaojia_shoucang',array('isshoucang'=>$shoucang['isshoucang']?0:1),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'listid'=>$listid));
		exi(!$shoucang['isshoucang']?'收藏成功！':'已取消收藏！');
	}
}
if($_RQ['op']=='upad_log'){
    $post=get_postdata();
    if($post['listid']){
        $this->model('model')->add_log($post['listid'],$post['adwei'],2);
    }
    exi('更新成功！');
}
if($_RQ['op']=='fub'){
    $listid=intval($_RQ['listid']);
    if($listid){
		$member=model('core/member')->detail($_SESSION['uid'],array('credit1','credit2','credit3','credit4','credit6','mobile','avatar','nickname'));
		if($this->setting['kan_bfee'] > $member['credit3']){
			exi('您的金币不足，不能参与！','error');
		}
		if($this->setting['kan_bfee']>0){
			model('core/member')->member_credit($_SESSION['uid'],'credit3',$this->setting['kan_bfee'],false,'jy_shopbaojia','查看报价扣金币');
		}
        $this->model('model')->add_log($listid,0,1);
    }else{
		 exi('选择要查看的商品！','error');
	}
    exi('查看报价成功！');
}